package com.mjw.zstu.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mjw.zstu.entity.Company;
import com.mjw.zstu.mapper.CompanyMapper;
import com.mjw.zstu.resultDto.JsonResult;
import org.apache.ibatis.annotations.Select;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CompanyService {
    @Autowired
    CompanyMapper companyMapper;
    public Company login(Company company){
        return companyMapper.selectOne(new QueryWrapper<>(company));
    }
    public int register(Company company){
        company.setIs_review(0);
        return companyMapper.insert(company);
    }
    public JsonResult update(Company company){
        companyMapper.updateById(company);
        JsonResult j=company_detail(company.getCompany_id());
        return j;
    }
    public JsonResult comparePassword(int company_id,String password){
       Company company = companyMapper.selectById(company_id);
       if(password.equals(company.getPassword())){
           return new JsonResult(200,"校验成功",null);
       }
       return new JsonResult(500,"校验失败",null);
    }
    public JsonResult company_detail(int id){
        Company company=new Company();
        company.setCompany_id(id);
        QueryWrapper<Company> companyQueryWrapper=new QueryWrapper<>(company);
        companyQueryWrapper.select(Company.class,i->!i.getColumn().equals("password"));
        return new JsonResult(200,"获取成功", companyMapper.selectOne(companyQueryWrapper));
    }
    public Company find(Company company){
        return companyMapper.selectOne(new QueryWrapper<>(company));
    }
   public IPage<Company> getCompanyList(int pageNum,int pageSize){
       Page page=new Page<Company>(pageNum,pageSize);
       QueryWrapper<Company> companyQueryWrapper=new QueryWrapper<>();
       companyQueryWrapper.orderByAsc("is_review");
       companyQueryWrapper.select(Company.class,i->!i.getColumn().equals("password"));
       return companyMapper.selectMapsPage(page,companyQueryWrapper);
   }
   public JsonResult refuse(Company company){
        companyMapper.updateById(company);
        return new JsonResult(200,"拒绝成功",null);
   }
   public int review(Company company){
        company.setRefuse_reason("");
        return companyMapper.updateById(company);
   }

}
